package com.zsl.swing.redis.desktop.window.list;

import com.zsl.swing.redis.desktop.utils.CollectionUtils;

import javax.swing.*;
import java.util.List;
import java.util.Vector;

public class ZslListModel extends AbstractListModel<ZslListNode>{

    private Vector<ZslListNode> list;

    public ZslListModel(){
        this.list = new Vector<>();
    }

    public Vector<ZslListNode> getList() {
        return list;
    }

    public void setList(Vector<ZslListNode> list) {
        this.list = list;
    }

    @Override
    public int getSize() {
        return this.list.size();
    }

    @Override
    public ZslListNode getElementAt(int index) {
        return this.list.get(index);
    }

    public void addOneElement(ZslListNode element){
        this.list.add(element);
        this.fireIntervalAdded(this, 0, 0);
    }

    public void addElementList(List<ZslListNode> elementList){
        if(!CollectionUtils.isEmpty( elementList)){
            this.list.addAll(elementList);

            this.fireIntervalAdded(this, 0, this.list.size());
        }
    }

    public void removeOneElement(ZslListNode element){
        int index = this.list.indexOf(element);
        this.list.remove(index);

        this.fireIntervalRemoved(this, index, index);
    }

    public void removeAllElementList(){
        int size = this.list.size();

        this.list.clear();

        this.fireIntervalRemoved(this, 0, size);
    }

}
